package com.jianglin.controller;

import com.jianglin.entity.HouseHoldInfo;
import com.jianglin.service.HouseHoldInfoService;
import com.jianglin.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author jianglin
 * 对小区住户实现增删改查操作
 */
@CrossOrigin(origins = "http://localhost:5500")
@RestController
public class HouseHoldInfoController {
    @Autowired
    private HouseHoldInfoService houseHoldInfoService;
    @GetMapping("/householdinfos")
    public R getExperiments( @RequestParam(required = false,defaultValue = "") String householdName,int page,int limit){
        return R.ok(houseHoldInfoService.findAllHouseHoldInfos(householdName,page,limit));
    }
    @PostMapping("/houselodinfo")
    public R saveExperiment(@RequestBody HouseHoldInfo houseHoldInfo){
        int ret = houseHoldInfoService.saveHouseHoldInfo(houseHoldInfo);
        if(ret > 0){
            return R.ok(ret);
        }else {
            return R.error("添加住户信息失败");
        }
    }
    @PutMapping("/householdinfo")
    public R updateExperiment(@RequestBody HouseHoldInfo houseHoldInfo){
        int ret = houseHoldInfoService.updateHouseHoldInfo(houseHoldInfo);
        if(ret > 0){
            return R.ok(ret);
        }else {
            return R.error("修改实验失败");
        }
    }
    @DeleteMapping("/householdinfo/{id}")
    public R deleteExperiment(@PathVariable int id){
        int ret = houseHoldInfoService.delHouseHoldInfoById(id);
        if(ret > 0){
            return R.ok(ret);
        }else {
            return R.error("删除失败");
        }
    }
    @DeleteMapping("/householdinfos")
    public R deleteExperimentsByIds(@RequestBody List<Integer> ids){
        int ret = houseHoldInfoService.delHouseHoldInfoByIds(ids);
        if(ret > 0){
            return R.ok(ret);
        }else {
            return R.error("删除失败");
        }
    }
}
